package com.heima.controller;


import com.heima.exception.customException;
import com.heima.pojo.Emp;
import com.heima.pojo.PageBean;
import com.heima.pojo.Result;
import com.heima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       HttpServletRequest request) {
//        String token = request.getHeader("token");
//        if (true){
//            throw new customException("大傻逼");
//        }
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize,name, gender, begin, end);
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("修改部门");
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息 : {}", emp);
        empService.update(emp);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除员工");
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result upload(@RequestBody Emp emp){
        log.info("增加员工");
        empService.add(emp);
        return Result.success();
    }
}
